#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "user.h"
#include "room.h"
#include "bill.h"
#include "customer.h"


int adminMenu()
{
	int c;
	showTime(1);
	printf("\t\t\t        旅店管理系统\n");
	printf("\t\t\t=============================\n");
	printf("\t\t\t      程序管理猿  系统\n");
	printf("\t\t\t   * 1 浏览用户\n");
	printf("\t\t\t   * 2 增加用户\n");
	printf("\t\t\t   * 3 删除用户\n");
	printf("\t\t\t   * 4 初始化用户密码\n");
	printf("\t\t\t   * 5 修改个人信息\n");
	printf("\t\t\t   * 6 系统备份\n");
	printf("\t\t\t   * 0 注销\n");
	printf("\t\t\t=============================\n");
	printf("\t\t\t请选择:");
	scanf("%d",&c);
	getchar();
	while(c < 0||c > 6)
	{
		printf("输入错误，请重新输入:");
		scanf("%d",&c);
		getchar();
	}
	return c ;


}


void adminSystem(User *r)
{
	int c,loop = 1;
	while(loop)
	{
		system("clear");
		//printf("\n%s",r->name);
		//showTime(1);
		c = adminMenu();
		switch(c)
		{
			case 1:
				printf(RED);
				printf("浏览用户\n");
				printf(NONE);
				r = read_user();
				display_user(r);
				printf("输入任意指令以继续:\n");
				getchar();
				break;
			case 2:
				printf(RED);
				printf("增加用户\n");
				printf(NONE);
				add_user();
				break;
			case 3:
				printf(RED);
				printf("删除用户\n");
				printf(NONE);
				remove_user();
				break;
			case 4:
				printf(RED);
				printf("初始化用户密码\n");
				printf(NONE);
				srtup_user_password();
				break;
			case 5:
				printf(RED);
				printf("修改个人信息\n");
				printf(NONE);
				edit_user_information();
				break;
			case 6:
				printf(RED);
				printf("系统备份\n");
				printf(NONE);
				backup_Sysyem();
				break;
			case 0:
			default:
				loop = 0;
				break;
		}//switch(c)
	}
	
}
void backup_Sysyem()
{
	int choose_save;  
	printf("请选择你要保存的文件所对应的编号:\n");
	printf("1.user.txt  2.room.txt  3.customer.txt  4.bill.txt\n");
	scanf("%d",&choose_save);
	getchar();
	switch(choose_save)
	{	
		case 1:
			backup_User();
			break;
		case 2:
			backup_room();	
			break;
		case 3:
			backup_customer();
			break;
		case 4:	
			backup_bill();
			break;
		case 0:
		default:
			break;
	}
	return ;
}

void backup_User()
{
	struct tm *ptr;
	time_t it;
	time(&it);
	ptr = localtime(&it);
	FILE *fp;
	User *head,*r;
	char s[30] ;  //now use 30 for not too short
	head = read_user();
	r = head;
	sprintf(s,"user%d/%d/%d %d:%d:%d.txt",ptr->tm_year+1900,ptr->tm_mon+1,ptr->tm_mday,ptr->tm_hour,ptr->tm_min,ptr->tm_sec);
//	printf("%s\n",s);
	fp = fopen(s,"w");
	if (fp == NULL)
	{
		perror("open new user file");
		return ;
	}
	while(r != NULL)
	{
		fprintf(fp,"%-12s",r->userId);
		fprintf(fp, "%-7s",r->password);
		fprintf(fp, "%-9s",r->name);
		fprintf(fp, "%-4s",r->sex);
		fprintf(fp, "%-8s",r->role);
		fprintf(fp, "%-20s\n",r->email);
		r = r->next;
	}	
	if (fp)
		fclose(fp);
	printf("输入任意指令以继续\n");
	getchar();

	return ;
	
	
}

void backup_room()
{
	struct tm *ptr;
	time_t it;
	time(&it);
	ptr = localtime(&it);
	FILE *fp;
	Room *head,*r;
	char s[30] ;  //now use 30 for not too short
	head = read_room();
	r = head;
	sprintf(s,"room%d/%d/%d %d:%d:%d.txt",ptr->tm_year+1900,ptr->tm_mon+1,ptr->tm_mday,ptr->tm_hour,ptr->tm_min,ptr->tm_sec);
//	printf("%s\n",s);
	fp = fopen(s,"w");
	if (fp == NULL)
	{
		perror("open new room file");
		return ;
	}
	while(r != NULL)
	{
		fprintf(fp,"%2d",r->roomId);
		fprintf(fp, "%-15s",r->name);
		fprintf(fp, "%5d",r->price);
		fprintf(fp, "%2d",r->discount);
		fprintf(fp, "%5d",r->cashPledge);
		fprintf(fp, "%2d\n",r->status);
		r = r->next;
	}	
	if (fp)
		fclose(fp);
	printf("输入任意指令以继续\n");
	getchar();

	return ;	
}

void backup_customer()
{
	struct tm *ptr;
	time_t it;
	time(&it);
	ptr = localtime(&it);
	FILE *fp;
	customer *head,*r;
	char s[30] ;  
	head = read_customer();
	r = head;
	sprintf(s,"cusmer%d/%d/%d %d:%d:%d.txt",ptr->tm_year+1900,ptr->tm_mon+1,ptr->tm_mday,ptr->tm_hour,ptr->tm_min,ptr->tm_sec);

	fp = fopen(s,"w");
	if (fp == NULL)
	{
		perror("open new customer file");
		return ;
	}
	while(r->next)
	{
		fprintf(fp,"%-2d",r->roomId);
		fprintf(fp, "%-19s",r->m_ID);
		fprintf(fp, "%-9s",r->m_name);
		fprintf(fp, "%-2d",r->m_age);
		fprintf(fp, "%-4s",r->m_sex);
		fprintf(fp, "%-12s",r->phone);
		fprintf(fp, "%-12s",r->inDay);
		fprintf(fp, "%-12s",r->outDay);
		fprintf(fp, "%-2d\n",r->status);
		r = r->next;
	}	
	if (fp)
		fclose(fp);
	printf("输入任意指令以继续\n");
	getchar();

	return ;	
}

void backup_bill()
{
	struct tm *ptr;
	time_t it;
	time(&it);
	ptr = localtime(&it);
	FILE *fp;
	bill *head,*r;
	char s[30] ;  
	head = read_bill();
	r = head;
	sprintf(s,"bill%d/%d/%d %d:%d:%d.txt",ptr->tm_year+1900,ptr->tm_mon+1,ptr->tm_mday,ptr->tm_hour,ptr->tm_min,ptr->tm_sec);

	fp = fopen(s,"w");
	if (fp == NULL)
	{
		perror("open new bill file");
		return ;
	}
	while(r->next)
	{
		fprintf(fp,"%2d",r->roomId);
		fprintf(fp, "%-4d",r->price);
		fprintf(fp, "%-4d",r->yingPrice);
		fprintf(fp, "%-4d",r->realPrice);
		fprintf(fp, "%-4d",r->cashPledge);
		fprintf(fp, "%-4d",r->change);
		fprintf(fp, "%1d",r->status);
		fprintf(fp, "%-12s",r->inDay);
		fprintf(fp, "%-12s\n",r->outDay);
		r = r->next;
	}	
	if (fp)
		fclose(fp);
	printf("输入任意指令以继续\n");
	getchar();

	return ;	
}
